package org.wsiz.vro.web.domain;

import static org.wsiz.vro.domain.Account_.*;
import static com.google.common.collect.Lists.newArrayList;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.wsiz.vro.domain.Account;
import org.wsiz.vro.repository.AccountRepository;
import org.wsiz.vro.repository.support.Range;
import org.wsiz.vro.repository.support.Ranges.RangeDate;
import org.wsiz.vro.web.domain.support.GenericSearchForm;

/**
 * SearchForm for {@link Account}
 */
@Component
@Scope("session")
public class AccountSearchForm extends GenericSearchForm<Account> implements Serializable {
    private static final long serialVersionUID = 1L;
    // make it static to avoid http://jira.springframework.org/browse/SWF-1224
    private static AccountRepository accountRepository;

    private Account account = new Account();
    private RangeDate<Account> birthDateRange = new RangeDate<Account>(birthDate);

    @Autowired
    public AccountSearchForm(AccountRepository instance) {
        if (accountRepository == null) {
            accountRepository = instance;
        }
    }

    /**
     * Server side pagination with lazy model.
     * Automatically called by PrimeFaces component (via the GenericSearchForm).
     */
    @Override
    protected List<Account> load(PageRequest pageRequest) {
        List<Range<Account, ?>> ranges = newArrayList();
        ranges.add(birthDateRange);

        Page<Account> page = accountRepository.findWithExample(account, ranges, pageRequest);
        setRowCount((int) page.getTotalElements());
        return page.getContent();
    }

    /**
     * The root account for search by example.
     * Used from the view.
     */
    public Account getAccount() {
        return account;
    }

    // Ranges, used from the view.

    public RangeDate<Account> getBirthDateRange() {
        return birthDateRange;
    }
}